home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.04 Apr 97 / TextObjects / Text Objects code
Encoding:
Text File  |  1997-02-21  |  3.5 KB  |  163 lines  |  [TEXT/CWIE]

  1. class TText : public CAbstractText    //Abstract text supplies all the other                                                                     // methods
  2.     {
  3.     
  4.     private:
  5.         TPrivateText     *fInternalText;
  6.     public:
  7.         void                    ActivateText (Boolean OnOff);
  8.         void                    Compact();
  9.         void                    UnCompact();
  10.         virtual void        Update(RgnHandle updateRgn);
  11.         }
  12.  
  13. void TText::ActivateText (Boolean OnOff)
  14.     {
  15.     if (OnOff)
  16.         {
  17.         UnCompact();        // transmutes from a string, if needed
  18.         FocusToDraw();
  19.         fInternalText->Activate(true);
  20.         }
  21.     else
  22.         {
  23.         FocusToDraw();
  24.         fInternalText->Activate(false);
  25.         Compact();            // attempts to transmute into a string
  26.         }
  27.     }
  28.  
  29. void TText::Compact()
  30.     {Handle itsText;
  31.     TInternalText *newText;
  32.     Point itsPenStart
  33.             
  34.     if (fInternalText->CanCompact())                            {
  35.         itsText= fInternalText->GetText();
  36.         itsPenStart= fInternalText->GetPenStart();
  37.         newText= new TString(itsText,itsPenStart);
  38.         delete fInternalText;
  39.         fInternalText=newText;
  40.         }
  41.     }
  42.  
  43. void TText::UnCompact()
  44.     {Handle itsText;
  45.     TInternalText *newText;
  46.             
  47.     if (fInternalText->CanExpand())                                {
  48.         itsText= fInternalText->GetText();
  49.         newText= new TStyledText(itsText);
  50.         delete fInternalText;
  51.         fInternalText=newText;
  52.         }
  53.     }
  54.  
  55. void TText::Update(RgnHandle updateRgn)
  56.     {
  57.     
  58.     if (RectInRgn(&fBoundingBox, updateRgn))
  59.          {
  60.          FocusToDraw();
  61.         Frame();
  62.         fInternalText->Update(updateRgn);
  63.          }
  64.     }    
  65.  
  66.  
  67.  
  68. class TInternalText                 //important fields and methods only
  69.     {
  70.     virtual void         ActivateText (Boolean OnOff) {;};
  71.     virtual Boolean     CanCompact() {return false};
  72.     virtual Boolean     CanExpand() {return false};
  73.     virtual Handle     GetText();
  74.     virtual Point        GetPenStart(){return PointOf(0,0)};
  75.     virtual void            SetPenStart(Point aStart){;};
  76.     virtual void         Update(RgnHandle updateRgn);
  77.     }
  78.  
  79. class TString    : public TInternalText    //important fields and methods                                                                         //only        
  80.     {
  81.     Handle fText;
  82.     Point fPenStart;
  83.  
  84.     Boolean             CanExpand() {return true};
  85.     Handle                 GetText() {return fText};
  86.     void                    SetPenStart(Point aStart){fPenStart=aStart};
  87.     void                     Update(RgnHandle updateRgn);
  88.     }
  89.  
  90. class TStyledText    : public TInternalText    //important fields and                                                                                     //methods only 
  91.     {
  92.     void                 ActivateText (Boolean OnOff);
  93.     Boolean         CanCompact();
  94.     virtual Point    GetPenStart(){return PointOf(0,0)};
  95.     Handle             GetText();
  96.     void                 Update(RgnHandle updateRgn);
  97.     }
  98.  
  99.  
  100. void TString::Update(RgnHandle updateRgn)
  101.     {
  102.     MoveTo(fPenStart.h,fPenStart.v);
  103.     HLock(fText);        
  104.     DrawText( *fText, 0, GetHandleSize (fText) ); //Toolbox Routine    HUnlock( fText );
  105.     }
  106.  
  107.  
  108. Boolean TStyledText::CanCompact()
  109.     {
  110.     long oldStart,oldEnd,length;
  111.     long kMaxString=999;
  112.     SignedByte alignment;
  113.     Boolean canDo=false;
  114.  
  115.     length= WEGetTextLength(fMacWE);
  116.             
  117.     WEGetSelection(&oldStart,&oldEnd,fMacWE);
  118.         
  119.     if ((oldEnd-oldStart)<kMaxString)                // not too long
  120.         {
  121.         WESetSelection(0,kMaxString,fMacWE);
  122.             
  123.         mode = doFont + doFace + doSize + doColor;
  124.             
  125.         alignment=WEGetAlignment(fMacWE);
  126.     
  127.         if ((WEContinuousStyle(&mode, &aStyle, fMacWE))  &&
  128. // one style
  129.             (WEOffsetToLine(kMaxString, fMacWE)==0) &&    
  130. // one line            
  131.             ((alignment== weFlushLeft) ||(alignment==                 weFlushDefault))    
  132. // no fancy  alignment
  133.             )
  134.             canDo=true;
  135.         
  136.         WESetSelection(oldStart,oldEnd,fMacWE);
  137.         }
  138. else
  139.     return canDo        
  140.     }
  141.  
  142. Point TStyledText::GetPenStart()
  143.     {Point penStart;
  144.     short lineAscent,lineDescent;
  145.     LongRect destRect;
  146.             
  147.     WEGetDestRect(&destRect,fMacWE);    
  148.     
  149.     penStart.h=destRect.left;    
  150.     
  151.     _WECalcHeights(0, 1, &lineAscent, &lineDescent, fMacWE);
  152.                 
  153.     penStart.v= destRect.top+lineAscent;
  154.                 
  155.     return
  156.         penStart
  157.     }
  158.  
  159. void TStyledText::Update(RgnHandle updateRgn)
  160.     {
  161.     WEUpdate(updateRgn, fMacWE); // passed to the styled text engine
  162.     }
  163.